#include "messageitem.h"
#include"modle/data.h"
#include <QGridLayout>
#include <QLabel>
#include <QPainter>
#include <QPainterPath>
#include <QPushButton>
#include"userinforwidget.h"
using module::Message;
using module::MessageType;
MessageItem *MessageItem::makeMessageItem(bool isleft, const Message &mess)
{
    //创建我们的布局管理器
    QGridLayout * layout=new QGridLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    MessageItem*ret=new MessageItem(isleft);
    ret->setObjectName("messageitemwidget");
    ret->setStyleSheet("#messageitemwidget{background:transparent}");
    ret->setLayout(layout);
    //创建我们的头像按钮
    QPushButton *firstavator=new QPushButton();
    firstavator->setIcon(QIcon(":/Resources/avator1.jpg"));
    firstavator->setFixedSize(50,50);
    firstavator->setIconSize(QSize(30,30));
    firstavator->setStyleSheet(
        "QPushButton {"
        "   border: none;"
        "   border-radius: 12px;"
        "   background: qlineargradient(135deg, #ff6b6b, #ffa36c);"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(135deg, #ff8e8e, #ffb78c);"
        "   border: 2px solid rgba(255, 255, 255, 0.3);"
        "}"
        );
    if(isleft)
    {
        layout->addWidget(firstavator,0,0,2,1,Qt::AlignTop|Qt::AlignLeft);
    }
    else
    {
        layout->addWidget(firstavator,0,1,2,1,Qt::AlignTop|Qt::AlignRight);
    }
    //创建我们的显示名字和时间的label
    QLabel *secondWidget=new QLabel();
    secondWidget->setText(mess.sender.name+" | "+mess.time);
    secondWidget->setStyleSheet("QLabel { font-size: 12px; color: rgb(178, 178, 178); }");
    if(isleft)
    {
        layout->addWidget(secondWidget,0,1);
    }
    else
    {
        layout->addWidget(secondWidget,0,0,Qt::AlignRight);
    }
    //创建正文消息正文内容的控件
    QWidget*thirdcontextwidget=new QWidget();
    switch(mess.messageType)
    {
    case MessageType::TEXT_TYPE:
        thirdcontextwidget=MessageItem::makeTextMessageItem(isleft,QString::fromUtf8(mess.context));
        break;
    case MessageType::FILE_TYPE:
        break;
    case MessageType::IMAGE_TYPE:
        break;
    case module::MessageType::SPEECH_TYPE:
        break;
    default:
        LOG()<<"unknown information";
        break;
    }
    if(isleft)
    {
        layout->addWidget(thirdcontextwidget,1,1);
    }
    else
    {
        layout->addWidget(thirdcontextwidget,1,0);
    }
    //设置头像按钮的信号和槽函数
    connect(firstavator,&QPushButton::clicked,ret,[](){
        UserInforWidget * wid=new UserInforWidget();
        wid->exec();
    });
    return ret;
}

QWidget *MessageItem::makeTextMessageItem( bool isleft,const QString &text)
{
    //在这里创建一个自定义控件类并加入到我们的布局管理
    QWidget *widget=new TextLable(isleft,text);
    return widget;
}

MessageItem::MessageItem(bool isleft,QWidget *parent)
    : QWidget(parent)
{
    this->isleft = isleft;
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

TextLable::TextLable(bool isleft, QString text, QWidget *parent)
    : QWidget(parent), isleft(isleft)
{
    // 设置一下 SizePolicy
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QFont font;
    font.setFamily("微软雅黑");
    font.setPixelSize(16);

    this->label = new QLabel(this);
    this->label->setText(text);
    this->label->setFont(font);
    this->label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    this->label->setWordWrap(true);    // 设置文本自动换行
    this->label->setStyleSheet("QLabel { padding: 0 10px; line-height: 1.2; background-color: transparent; }");


}

void TextLable::paintEvent(QPaintEvent *event)
{
    (void) event;

    // 1. 获取到父元素的宽度
    QObject* object = this->parent();
    if (!object->isWidgetType()) {
        // 当前这个对象的父元素不是预期的 QWidget, 此时不需要进行任何后续的绘制操作.
        return;
    }
    QWidget* parent = dynamic_cast<QWidget*>(object);
    int width = parent->width() * 0.7;

    // 2. 计算当前文本, 如果是一行放置, 需要多宽.
    QFontMetrics metrics(this->label->font());
    int totalWidth = metrics.horizontalAdvance(this->label->text());

    // 3. 计算出此处的行数是多少 (40 表示左右各有 20px 的边距)
    int rows = (totalWidth / (width - 40)) + 1;
    if (rows == 1) {
        // 如果此时得到的行数就只有一行
        width = totalWidth + 40;
    }

    // 4. 根据行数, 计算得到高度. (20 表示上下各有 10px 的边距)
    int height = rows * (this->label->font().pixelSize() * 1.2 ) + 20;

    // 5. 绘制圆角矩形和箭头
    QPainter painter(this);
    QPainterPath path;
    // 设置 "抗锯齿"
    painter.setRenderHint(QPainter::Antialiasing);
    if (isleft) {
        painter.setPen(QPen(QColor(255, 255, 255)));
        painter.setBrush(QColor(255, 255, 255));

        // 绘制圆角矩形
        painter.drawRoundedRect(10, 0, width, height, 10, 10);
        // 绘制箭头
        path.moveTo(10, 15);
        path.lineTo(0, 20);
        path.lineTo(10, 25);
        path.closeSubpath();   // 绘制的线形成闭合的多边形, 才能进行使用 Brush 填充颜色.
        painter.drawPath(path);

        this->label->setGeometry(10, 0, width, height);
    } else {
        painter.setPen(QPen(QColor(137, 217, 97)));
        painter.setBrush(QColor(137, 217, 97));

        // 圆角矩形左侧边的横坐标位置
        int leftPos = this->width() - width - 10; // 10 是用来容纳 箭头 的宽度
        // 圆角矩形右侧边的横坐标位置
        int rightPos = this->width() - 10;
        // 绘制圆角矩形
        painter.drawRoundedRect(leftPos, 0, width, height, 10, 10);
        // 绘制箭头
        path.moveTo(rightPos, 15);
        path.lineTo(rightPos + 10, 20);
        path.lineTo(rightPos, 25);
        path.closeSubpath();
        painter.drawPath(path);

        this->label->setGeometry(leftPos, 0, width, height);
    }

    // 6. 重新设置父元素的高度, 确保父元素足够高, 能够容纳下上述绘制的消息显示的区域
    //    注意高度要涵盖之前名字和时间的 label 的高度, 以及留点冗余空间.
    parent->setFixedHeight(height + 50);



}
